#pragma once

#include <string>

#include "noncopyable.h"

/// 使用do-while(0)避免宏定义造成意外的错误

#define LOG_INFO(logMsgFormat, ...)                 \
    do                                              \
    {                                               \
        Logger &logger = Logger::getInstance();     \
        logger.setLogLevel(INFO);                   \
        char buff[256] = {0};                       \
        sprintf(buff, logMsgFormat, ##__VA_ARGS__); \
        logger.log(buff);                           \
    } while (0)

#define LOG_ERROR(logMsgFormat, ...)                \
    do                                              \
    {                                               \
        Logger &logger = Logger::getInstance();     \
        logger.setLogLevel(ERROR);                  \
        char buff[256] = {0};                       \
        sprintf(buff, logMsgFormat, ##__VA_ARGS__); \
        logger.log(buff);                           \
    } while (0)
#define LOG_FATAL(logMsgFormat, ...)                \
    do                                              \
    {                                               \
        Logger &logger = Logger::getInstance();     \
        logger.setLogLevel(FATAL);                  \
        char buff[256] = {0};                       \
        sprintf(buff, logMsgFormat, ##__VA_ARGS__); \
        logger.log(buff);                           \
    } while (0)
#ifdef MUDEBUG
#define LOG_DEBUG(logMsgFormat, ...)                \
    do                                              \
    {                                               \
        Logger &logger = Logger::getInstance();     \
        logger.setLogLevel(DEBUG);                  \
        char buff[256] = {0};                       \
        sprintf(buff, logMsgFormat, ##__VA_ARGS__); \
        logger.log(buff);                           \
    } while (0)
#else
#define LOG_DEBUG(logMsgFormat, ...)
#endif

enum LogLevel
{
    INFO,  // 普通信息
    ERROR, // 错误信息
    FATAL, // core信息
    DEBUG, // 调试信息
};

/**
 * @brief 日志类
 *
 */
class Logger : noncopyable
{
public:
    //  获取单例
    static Logger &getInstance();
    // 设置日志级别
    void setLogLevel(int level);
    // 写日志
    void log(std::string msg);

private:
    int logLevel_;
};